#version 460
#define GLSLIFY 1

#define GLSLIFY 1
#define PROJECT_MATRIX

/*
0 投影矩阵
1 属性参数
2 变换数据
3 颜色数据

4 顶点数据
5 顶点配置参数


6 颜色控制参数

10 纹理数据

*/

layout(std140, set=0, binding=0) uniform U_Matrix {
	mat4 mat_ProjecView[2];
};

layout(std430, column_major, binding = 1) readonly buffer U1_Matrix {
	mat4 mat_Model[];
};

layout(std140, set=0, binding=2) uniform U3_Matrix {
	vec4 mat_LightDir[4];	
};

layout(std140, column_major, binding=4) uniform U4_Skeleton {
	//mat4 mat_ProjecView[2];
	uint m_VertexNum;
	uint m_BonesNum;
};

//骨骼对应变换
layout(std430, column_major, binding = 5) readonly buffer U_VertexSkeletonMatrix {
	mat4 m_SkeletonMatrix[];
};
//蒙皮权重 [层数_骨骼数量][对应每个顶点的权重]
layout(std430, column_major, binding = 6) readonly buffer U_VertexWeigth {
	float m_VertexWeigth[];
};
//顶点指向的骨骼ID 包含四个骨骼[0~254,255代表没有关联骨骼]
layout(std430, column_major, binding = 7) readonly buffer U7_VertexSkeleton {
	uint SkeletonID[];
};

 

layout(location = 0) in vec3 inPos;
layout(location = 1) in vec3 inNormal;
layout(location = 2) in vec2 inUV;

layout(location = 0) out vec3 vColor;
layout(location = 1) out vec3 vNormal;
layout(location = 2) out vec3 vLightPos[4];
out gl_PerVertex {
	vec4 gl_Position;
};

void main(void){
	mat4 bonesMat_som = mat_Model[gl_InstanceIndex];
	//固定顶点坐标
	vec4 p = bonesMat_som * vec4(inPos, 1.0);
	vec4 a = p;

	mat3x3 normalMax = mat3x3(mat_Model[gl_InstanceIndex]);
	vNormal = normalize((normalMax * inNormal).xyz);
	vec3 n = vNormal;

	//mat4 bonesMat_A;
	//bonesMat_A[0] = vec4(1,0,0,0);
	//bonesMat_A[1] = vec4(0,1,0,0);
	//bonesMat_A[2] = vec4(0,0,1,0);
	//bonesMat_A[3] = vec4(0,0,0,1);
	if(m_BonesNum > 0){
		uint bones_bit = 255;
		uint bonesID = 0;
		bonesID = SkeletonID[gl_VertexIndex];
		
		for(uint i=0; i<4; ++i){
			bones_bit = (bonesID >> (8*i) ) & (0x000000ff);
			if(bones_bit < 255){
				float weigth = m_VertexWeigth[bones_bit * m_VertexNum + gl_VertexIndex];
				
				mat4 mat = m_SkeletonMatrix[bones_bit + m_BonesNum];
				
				vec4 q = mat[1];
				//固定顶点到初始骨骼坐标
				vec3 p_pos = p.xyz - mat[3].xyz;
				//骨骼缩放顶点
				p_pos *= mat[0].xyz;
				//newPos.x = p.w * q.x + q.w * p.x + p.y * q.z - q.y * p.z;
				//newPos.y = p.w * q.y + q.w * p.y + p.z * q.x - q.z * p.x;
				//newPos.z = p.w * q.z + q.w * p.z + p.x * q.y - q.x * p.y;
				//newPos.w = q.w * q.w - q.x * q.x - q.y * q.y - q.z * q.z;
				//vec3 newPos = p_pos*(2.0f*q.w*q.w-1.0f) + cross(q.xyz, p_pos)*q.w*2.0f + (q.xyz)*dot((q.xyz), p_pos)*2.0f;
				vec3 newPos = p_pos*(2.0f*q.w*q.w-1.0f) - cross(q.xyz, p_pos)*q.w*2.0f + q.xyz*dot(q.xyz, p_pos)*2.0f;
				vec3 newNormal = vNormal*(2.0f*q.w*q.w-1.0f) - cross(q.xyz, vNormal)*q.w*2.0f + q.xyz*dot(q.xyz, vNormal)*2.0f;
				
				//旋转后的固定顶点加上变换后骨骼坐标
				newPos += mat[2].xyz;
				a.xyz = mix(a.xyz, newPos, weigth);
				n = mix(n, newNormal, weigth);
				//p.xyz = newPos;
				//break;
			}

			if(bones_bit == 3){
				vColor = vec3(1,0,0);
			}
			if(bones_bit == 4){
				vColor = vec3(0,1,0);
			}
			if(bones_bit == 5){
				vColor = vec3(0,0,1);
			}
			if(bones_bit == 6){
				vColor = vec3(1,1,0);
				break;
			}
		}
		//p *= m_SkeletonMatrix[3];
	}
	
	//float weigth = m_VertexWeigth[10 * m_VertexNum + gl_VertexIndex];
	//vColor = vec3(weigth, 0, 0);
	p = a;
	vNormal = n;
	//bonesMat_som *= mat_Model[gl_InstanceIndex];
	gl_Position = mat_ProjecView[0] * p;
	//gl_Position = vec4(inPos, 1.0);
	//vColor = inColor;
	
	//vNormal = normalize(inNormal);
	//vLightPos[0] = (inverse(mat_Model[gl_InstanceIndex]) * mat_LightDir[0]).xyz;
	vLightPos[0] = inverse(mat3(mat_ProjecView[1])) * vec3(0.0, 0.0, -1.0);
	vLightPos[1] = normalize(vLightPos[0]);
	vLightPos[2] = normalize(vec3(1.0, 1.0, 0.0));
}

 